# -*- coding: utf-8 -*-
# coding=utf-8
'''
Created on 2014-3-20
首页生成分类html列表
@author: SimonFu
'''
import os
import sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)

import codecs
import csv
import DBCenter



'''
    导入category
'''
def RunGenerate():
    get_category_sql = '''
        select * from category
    '''
    all_category_datas = DBCenter.rc_db.query(get_category_sql)
    
    html = [];
    html.append('<ul>')
    
    #1级分类
    category1_datas = [i for i in all_category_datas if(i.get("category_level") == 1)]
    
    for category1_data in category1_datas:
        
        #判断1级分类是否存在商品
        style_category1_data_sql = '''
            select * from style where category_id1 = '%s'
        ''' % category1_data.get('category_id')
        style_category1_data = DBCenter.rc_db.query(style_category1_data_sql)
        if len(style_category1_data) == 0:
            continue
        
        html.append('<li class="item-tb">')
        html.append('    <div class="toptag">')
        html.append('        <i class="icon-arrow-left"></i>')
        html.append('        <dl class="">')
        html.append('            <dt><a href="/List/' + category1_data.get('category_id') + '">' + category1_data.get('category_name') + '</a></dt>')
        html.append('            <dd>')
        #2级分类
        category2_datas = [i for i in all_category_datas if(i.get("category_level") == 2 and i.get("parent_id") == category1_data.get('category_id'))]
        category2_html1 = []
        for category2_data in category2_datas:
            #判断2级分类是否存在商品
            style_category2_data_sql = '''
                select * from style where category_id2 = '%s'
            ''' % category2_data.get('category_id')
            style_category2_data = DBCenter.rc_db.query(style_category2_data_sql)
            if len(style_category2_data) == 0:
                continue
            
            category2_html1.append('                <a href="/List/' + category2_data.get('category_id') + '">' + category2_data.get('category_name') + '</a>')
        
        html.append(' |\r\n'.join(category2_html1));
       
        html.append('            </dd>')
        html.append('        </dl>')
        html.append('    </div>')
        html.append('    <div class="drop-box item-item-tb">')
        html.append('        <dl>')
        html.append('            <dt>' + category1_data.get('category_name') + '</dt>')
        html.append('            <dd>')
        
        category2_html2 = []
        for category2_data in category2_datas:
            #判断2级分类是否存在商品
            style_category2_data_sql = '''
                select * from style where category_id2 = '%s'
            ''' % category2_data.get('category_id')
            style_category2_data = DBCenter.rc_db.query(style_category2_data_sql)
            if len(style_category2_data) == 0:
                continue
            category2_html2.append('                <a href="/List/' + category2_data.get('category_id') + '">' + category2_data.get('category_name') + '</a>')
            
        html.append('\r\n'.join(category2_html2));
        html.append('            </dd>')
        html.append('        </dl>')
        html.append('    </div>')
        html.append('</li>')
    
    html.append('</ul>')
    
    print '\r\n'.join(html)
        
def Run():
    RunGenerate()
    
Run()
# print os.getcwd()
